package com.ruoyi.web.controller.biz.hugong.openapi;

import com.ruoyi.system.service.impl.WxPayService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName PayOpenApi
 * @Description
 * @Author sizegang
 * @Date2025/4/22 22:17
 **/
@RestController
@RequestMapping("/openapi/pay")
public class PayOpenApi {
    @Autowired
    private WxPayService wxPayService;

    @PostMapping("/create")
    public ResponseEntity<?> createPayment(@RequestBody PaymentRequest paymentRequest,
                                           HttpServletRequest request) {
        try {
            String clientIp = request.getRemoteAddr();
            Map<String, String> payParams = wxPayService.createOrder(
                    paymentRequest.getOpenid(),
                    paymentRequest.getOrderNo(),
                    paymentRequest.getBody(),
                    paymentRequest.getTotalFee(),
                    clientIp
            );
            return ResponseEntity.ok(payParams);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", e.getMessage()));
        }
    }

    @Data
    public static class PaymentRequest {
        private String openid;     // 用户openid
        private String orderNo;   // 商户订单号
        private String body;      // 商品描述
        private int totalFee;     // 订单金额（单位：分）
    }
}
